﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using AyFrame.Common;
using AyFrame.DbEntity.Models;
using AyFrame.Dto;
using AyFrame.IServices;

namespace AyFrame.Services
{
    public class AuthService : BaseService<SysUserInfo>, IAuthService
    {
        public AuthService()
        {

        }

        public async Task<LoginResponse> Token(LoginRequest req)
        {
            var user = await unitOfWork.Queryable<SysUserInfo>()
                .Where(t => t.LoginName == req.LoginName)
                .FirstAsync();

            if (user == null) return new LoginResponse { IsSuccess = false, ErrMsg = "用户不存在" };

            var pwd = Utils.MD5(req.Password + user.Salt);
            if (pwd != user.Password) return new LoginResponse { IsSuccess = false, ErrMsg = "登录信息错误" };

            return new LoginResponse
            {
                IsSuccess = true,
                ApiKey = Guid.NewGuid().ToString("n"),
                UserId = user.Id,
                Ip = req.Ip,
                UserName=user.Name
            };
        }
    }
}
